#include <stdio.h>
#include <stdlib.h>
#include "list.h"

int list_insert_forword(list **node, score *data)
{
	list *new = malloc(sizeof(*new));
	if (new == NULL)
		return -1;   
	new->data = *data;
	new->next = *node;    
	*node = new;
	return 0;            //成功则返回0
}

int list_insert_backword(list **node,score *data)
{
	list *new = malloc(sizeof(list));
	if (new == NULL)
		return -1;
	new->data = *data;
	new->next = NULL;
	list *current = *node;

	if (current == NULL)    //当链表为空的情况
	{
		*node = new;
		return 0;
	}
	while (current->next != NULL)
		current = current->next;
	current->next = new;
	return 0;
}

void list_show(list *node)
{
	list *current = node;
	while (current != NULL)
	{
		printf("%d %s %d %d\n", current->data.id, current->data.name,
		       current->data.math, current->data.chinese);
		current = current->next;
	}
}

int list_delete(list **node)
{
	if (*node == NULL)         //首先判断链表是否为空
		return -1;
	list *current = *node;
	*node = current->next;
	free(current);
	return 0;
}

score *list_find(list *node, int id)
{
	list *current = node;
	while (current)
	{
		if (current->data.id == id)
			return &current->data;
		current = current->next;
	}
	return NULL;
}

//销毁链表   ***需要关注***
void list_destory(list **node)
{
	list *current = *node;
	while (current)
	{
		list_delete(node);
		current = *node;
	}
	*node = NULL;
}